const MYSQL = require("mysql2");
let Context = {
  // 主机
  host: "mysql.sqlpub.com",
  // 端口
  port: "3306",
  // 用户名
  user: "tardeadmin",
  // 登录密码
  password: "4099bdc8bb99d226",
  // 数据库
  database: "tardeserver",
  waitForConnections: true,
  connectionLimit: 10,
  maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit`
  idleTimeout: 259200000, // idle connections timeout, in milliseconds, the default value 60000
  queueLimit: 0,
  enableKeepAlive: true,
  keepAliveInitialDelay: 0,
};
const SQL = MYSQL.createPool(Context);
// 输出连接状态
SQL.on("error", (err) => {
  if (err.code === "PROTOCOL_CONNECTION_LOST") {
    console.log("MYSQL 数据库连接断开，重新连接");
    SQL.connect((err) => {
      if (err) {
        console.log("MYSQL 数据库重新连接失败");
        console.log(err);
      } else {
        console.log("MYSQL 数据库重新连接成功");
      }
    });
  } else {
    console.log("MYSQL 数据库连接错误");
  }
});
SQL.on("connection", (strem) => {});
module.exports = SQL;
